package b4.view.component.town
{
	import as3isolib.core.*;
	import as3isolib.display.*;
	import as3isolib.display.primitive.*;
	import as3isolib.display.renderers.*;
	import as3isolib.display.scene.*;
	import as3isolib.geom.*;
	import as3isolib.graphics.*;
	
	import b4.config.Config;
	import b4.config.LayerConfig;
	import b4.config.LayerItemConfig;
	import b4.config.TownConfig;
	import b4.util.GameProfile;
	import b4.util.Resource;
	import b4.view.component.admin.Painter;
	import b4.view.component.basic.BasicWidget;
	import b4.view.component.events.TownSceneEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.getTimer;
	
	public class TownScene extends BasicWidget
	{
		public static const gridWidth:int = 18;
		private var townIndex:int;
		private var layerIndex:int;
		private var isoScene:IsoScene;
		private var isoView:IsoView;
		private var bgGrid:IsoSprite;
		private var roleSprites:Object;
//		private var mainRoleSprite:RoleSprite;
		private var coordinate:CoordinateGrid;
		private var _editMode:Boolean;
				
		private var mouseDownPos:Pt;
		private var mouseDownViewCenter:Pt;
		
		public function TownScene()
		{
			townIndex = -1;
			layerIndex = -1;
			//场景               
			isoScene = new IsoScene();                        
			isoScene.hostContainer = this;
			//坐标
			coordinate = new CoordinateGrid();
			coordinate.cellSize = gridWidth;
//			coordinate.setGridSize(50, 50, 1);
//			coordinate.showOrigin = true;
			//背景
			bgGrid = new IsoSprite();
			bgGrid.setSize(gridWidth, gridWidth, 0.1);
			bgGrid.moveBy(0, 0, 0);
			isoScene.addChild(bgGrid);
			
//			bgGrid.container.mouseEnabled = false;
//			bgGrid.container.mouseChildren = false;
			//角色
			roleSprites = new Object();
			//阴影
			var factory:as3isolib.core.ClassFactory = new as3isolib.core.ClassFactory(DefaultShadowRenderer);
			factory.properties = {shadowColor:0x000000, shadowAlpha:0.15, drawAll:false};                        
			isoScene.styleRenderers = [factory];                                                
			//设置视角   
			isoView = new IsoView();                        
			isoView.setSize(GameProfile.stageWidth, GameProfile.stageHeight);
			isoView.addScene(isoScene);
			isoView.centerOnPt(new Pt(300, 300));
			addChild(isoView);
			this.addEventListener(MouseEvent.CLICK, onMouseClick);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
//			this.mouseChildren = false;
		}
		
		private function onAddedToStage(event:Event):void
		{
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onRemovedFromStage(event:Event):void
		{
			this.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onMouseDown(evt:MouseEvent):void
		{
			if (editMode)
			{
				if (mouseDownPos == null)
				{
					mouseDownPos = new Pt(evt.stageX, evt.stageY);
					mouseDownViewCenter = viewCenter;
				}
			}
		}
		
		private function onMouseUp(evt:MouseEvent):void
		{
			mouseDownPos = null;
		}
		
		private function onMouseMove(e:MouseEvent):void
		{
//			if (e.buttonDown)
//				onMouseClick(e);
			if (editMode && Painter.penMode == null)
			{
				if (mouseDownPos != null && e.buttonDown)
				{
					const sensitivity:Number = 0.85;
					var diff:Pt = new Pt(e.stageX - mouseDownPos.x, e.stageY - mouseDownPos.y);
					trace("onMouseMove", diff);
					viewCenter = new Pt(mouseDownViewCenter.x - diff.x * sensitivity, mouseDownViewCenter.y - diff.y * sensitivity, mouseDownViewCenter.z);
				}
			}
		}
		
		private function onMouseClick(e:MouseEvent):void
		{
			var pt:Pt = new Pt(isoScene.container.mouseX, isoScene.container.mouseY);
			IsoMath.screenToIso(pt);
			if (editMode)
			{
				if (Painter.penMode != null)
				{
					var townConfig:TownConfig = Config.townConfigs[townIndex];
					if (townConfig != null)
					{
						var layerConfig:LayerConfig = townConfig.layers[layerIndex];
						if (layerConfig != null)
						{
							var startGridX:int = (pt.x < 0 ? pt.x - Painter.penSize * gridWidth: pt.x) / (Painter.penSize * gridWidth);
							var startGridY:int = (pt.y < 0 ? pt.y - Painter.penSize * gridWidth: pt.y) / (Painter.penSize * gridWidth);
							startGridX *= Painter.penSize;
							startGridY *= Painter.penSize;
							for (var i:int = 0; i < Painter.penSize; i++)
							{
								for (var j:int = 0; j < Painter.penSize; j++)
								{
									var gridX:int = startGridX + i;
									var gridY:int = startGridY + j;
									var oldItem:LayerItemConfig = layerConfig.map[gridX + " " + gridY];
									if (oldItem != null)
										oldItem.occPoints[gridX + " " + gridY] = null;
									var newItem:LayerItemConfig = Painter.penMode; 
									if (newItem.type == LayerItemConfig.TYPE_EMPTY)
									{
										layerConfig.map[gridX + " " + gridY] = null;
										newItem.occPoints[gridX + " " + gridY] = null;
									}
									else
									{
										layerConfig.setMapItem(gridX, gridY, newItem);
										newItem.occPoints[gridX + " " + gridY] = newItem;
									}
								}
							}
							coordinate.refreshTown(townIndex, layerIndex);
						}
					}
				}
			}
			var townSceneEvent:TownSceneEvent = new TownSceneEvent(TownSceneEvent.SCENE_MOUSE_CLICK);
			townSceneEvent.data = pt;
			dispatchEvent(townSceneEvent);
		}
		
		private function getCurrentLayerConfig():LayerConfig
		{
			var townConfig:TownConfig = Config.townConfigs[townIndex];
			return townConfig != null ? townConfig.layers[layerIndex] : null;
		}
		        
		public function refresh(townIndex:int, layerIndex:int = 0):void
		{
			if (this.townIndex != townIndex)
			{
				var townConfig:TownConfig = Config.townConfigs[townIndex];
				var TownClass:Class = Resource.getClassByName(townConfig.bgrImg);
				var townBgr:Sprite = new TownClass();
				townBgr.x = -townBgr.width / 2;
				bgGrid.sprites = [townBgr];
				this.townIndex = townIndex;
			}
			loadLayer(layerIndex);
		}
		
		public function loadLayer(layerIndex:int):void
		{
			this.layerIndex = layerIndex;
			if (coordinate != null)
				coordinate.refreshTown(townIndex, layerIndex);
		}
		
		public function addChildToScene(isoSprite:IsoSprite):void
		{
			if (!isoScene.contains(isoSprite))
				isoScene.addChild(isoSprite);
		}
		
		public function removeChildFromScene(isoSprite:IsoSprite):void
		{
			if (isoScene.contains(isoSprite))
				isoScene.removeChild(isoSprite);
		}
		
		public function get editMode():Boolean
		{
			return _editMode;
		}
		
		public function set editMode(flag:Boolean):void
		{
			_editMode = flag;
			if (_editMode)
			{
				isoScene.addChild(coordinate);
			}
			else
			{
				isoScene.removeChild(coordinate);
			}
		}
		
		public function get viewCenter():Pt
		{
			return isoView.currentPt;
		}
		
		public function set viewCenter(pt:Pt):void
		{
			isoView.centerOnPt(pt, false);
		}
		
//		
//		public function loadMainRoleSprite(role:Role):void
//		{
//			if (mainRoleSprite == null)
//			{
//				mainRoleSprite = new RoleSprite(role);
//				isoScene.addChild(mainRoleSprite);
//			}
//		}
		
		private function onEnterFrame(e:Event):void
		{
			isoScene.render();
		}

	}
}